import WebSocket from 'ws';

console.log('🔗 尝试连接到: ws://localhost:8000/ws/ai-chat/conversations/1/stream/');

const ws = new WebSocket('ws://localhost:8000/ws/ai-chat/conversations/1/stream/');

ws.on('open', function open() {
    console.log('✅ WebSocket连接成功建立！');
    
    // 发送心跳测试
    console.log('🏓 发送心跳测试...');
    ws.send(JSON.stringify({
        type: 'ping',
        timestamp: Date.now()
    }));
});

ws.on('message', function message(data) {
    console.log('📥 收到消息:', data.toString());
    
    try {
        const parsed = JSON.parse(data.toString());
        console.log('📋 消息类型:', parsed.type);
    } catch (e) {
        console.log('❌ JSON解析失败');
    }
});

ws.on('error', function error(err) {
    console.log('❌ WebSocket连接失败:', err.message);
});

ws.on('close', function close() {
    console.log('🔌 WebSocket连接已关闭');
});

// 5秒后关闭连接
setTimeout(() => {
    console.log('⏰ 5秒测试完成，关闭连接');
    ws.close();
    process.exit(0);
}, 5000);